大家好今天我們繼續來示範Dialog的其他用法,在這我們開一個新的空白專案來示範,首先一樣先上程式碼。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnSingleListDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SingleListDialog"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnMultipleListDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MultipleListDialog"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnSingleListDialog" />
</androidx.constraintlayout.widget.ConstraintLayout>
在xml新增了兩個按鈕,它們的功能分別是:
1.在很多個選項裡面選取一個項目,然後再按確定做下一個動作。
2.在很多個選項中可以選取多個項目,然後按下確定做下一個動作。
###MainActivity:
package com.example.day30_dialog_2;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mShowSingleChoiceDialog;
private Button mShowMultipleChoiceDialog;
int SingleCount = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mShowSingleChoiceDialog =findViewById(R.id.btnSingleListDialog);
mShowMultipleChoiceDialog =findViewById(R.id.btnMultipleListDialog);
mShowSingleChoiceDialog.setOnClickListener(this);
mShowMultipleChoiceDialog.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnSingleListDialog:
showSingleChoice();
break;
case R.id.btnMultipleListDialog:
showMultipleChoice();
break;
}
}
private void showMultipleChoice() {
final String[] english = {"A","B","C","D","E","F","G"};
final boolean[] select = new boolean[english.length];
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("複選的List")
.setMultiChoiceItems(english, select, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String result = "你選擇了:";
for (int i=0;i<select.length;i++){
if (select[i]){
result = result + "," + english[i];
}
}
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}
private void showSingleChoice() {
SingleCount = -1;
final String[] english = {"A","B","C","D","E","F","G"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("單選的List")
.setSingleChoiceItems(english, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SingleCount = which;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (SingleCount == -1){
Toast.makeText(MainActivity.this,"選取失敗,請重新選取",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"你選擇的是:"+english[SingleCount],Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}
}
我們來看看當按鈕按下去會做的動作的function:
1.showSingleChoice():
private void showSingleChoice() {
SingleCount = -1;
final String[] english = {"A","B","C","D","E","F","G"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("單選的List")
.setSingleChoiceItems(english, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SingleCount = which;
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (SingleCount == -1){
Toast.makeText(MainActivity.this,"選取失敗,請重新選取",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"你選擇的是:"+english[SingleCount],Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}
首先我們先設定一個陣列並且將我們要的資料放進去裡面,然後在.setSingleChoiceItems裡面放入陣列,在english逗號的旁邊設置-1是當我點擊單選的按鈕時,他會跳出完全沒有選取任何選項的Dialog,反之若我將它設定成1時,在一開始我點擊了單選的按鈕時,明明我還沒選擇任何選項,但是選項B已經被選取了,所以我將它預設為-1。
public void onClick(DialogInterface dialog, int which) {
SingleCount = which;
}
在一開始想必有些人會問為什麼要宣告一個int變數SingleCount等於-1,由上面的程式碼我們可以知道int which是代表我們點選到項目,我們使SingleCount等於which然後再Dialog按下確定按鈕時從SingleCount得知我們選擇的項目是多少,然後再使用Toast來讓使用者了解到他們選擇的項目是多少,最後再做一個防呆功能,這樣一個簡單的單選Dialog就完成了。
結果如下:
接下來繼續介紹再多個選項當中選取多個選項
private void showMultipleChoice() {
final String[] english = {"A","B","C","D","E","F","G"};
final boolean[] select = new boolean[english.length];
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("複選的List")
.setMultiChoiceItems(english, select, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String result = "你選擇了:";
for (int i=0;i<select.length;i++){
if (select[i]){
result = result + "," + english[i];
}
}
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.create()
.show();
}
在這和前一個Dialog差不多,先建立一個陣列將資料放進去,和上一個不同的是.setSingleChoiceItems變成了使用.setMultiChoiceItems,完成後結果如下:
今天就介紹到這,明天再繼續介紹其他的用法